package wahadlo.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

import wahadlo.controllers.AxisController;
import wahadlo.controllers.Controller;
import wahadlo.model.Model;
import wahadlo.model.ModelRecipient;
import wahadlo.model.ModelSupplier;
import wahadlo.model.StandardModel;

/**
 * Klasa reprezentuje koponent z przyciskami do zarządzania aplikacją.
 * 
 * @author Koziołek
 * 
 */
public class ControllPanel extends JComponent {
	private static final long serialVersionUID = 1L;
	private JButton start;
	private JButton stop;
	private JButton resume;
	private List<ModelSupplier> modelSuppliers;
	private List<ModelRecipient> modelRecipients;

	private boolean isResume;
	private AxisActionController axisActionController;

	public ControllPanel(List<ModelSupplier> modelSuppliers,
			List<ModelRecipient> modelRecipients) {
		this.modelSuppliers = modelSuppliers;
		this.modelRecipients = modelRecipients;
		init();
	}

	private void init() {
		start = new JButton("Start");
		axisActionController = new AxisActionController();
		start.addActionListener(axisActionController);
		stop = new JButton("Stop");
		stop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				start.setEnabled(true);
				AxisController axisController = (AxisController) axisActionController.axisController;
				axisController.getAnimation().setRunnig(false);
				setResume(false, axisController);
			}

		});

		resume = new JButton("Wyczyść");

		resume.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				start.setEnabled(true);
				AxisController axisController = (AxisController) axisActionController.axisController;
				axisController.getAnimation().setRunnig(false);
				for (ModelRecipient modelRecipient : modelRecipients) {
					modelRecipient.clean();
				}
			}
		});

		setLayout(new GridLayout(3, 1, 0, 2));
		add(start, 0);
		add(stop, 1);
		add(resume, 2);
	}

	private final class AxisActionController implements ActionListener {

		private Controller axisController;

		public void actionPerformed(ActionEvent e) {
			if (isResume) {
				AxisController axisController = (AxisController) axisActionController.axisController;
				setResume(false, axisController);
			} else {
				// blokujemy przycisk
				start.setEnabled(false);
				// przygotowujemy dane
				Model model = prepareModel();
				axisController = new AxisController();
				// wiążemy odbiorców z kontrolerem.
				for (ModelRecipient modelRecipient : modelRecipients) {
					if (modelRecipient instanceof AxisComponent) {
						((AxisComponent) modelRecipient).clean();
					}
					axisController.putRecipient(modelRecipient);
					axisController.putModel(model);
				}
				// wywołujemy wiązanie danych z obiektami w kontrolerze
				if (axisController.bind()) {
					// jak się powiedzie zaczynamy procesować dane
					axisController.process();
				} else {
					// jak nie zwracamy błąd i odblokowujemy przycisk
					JOptionPane.showMessageDialog(null, "Podałeś będne dane.",
							"Błąd", JOptionPane.ERROR_MESSAGE);
					start.setEnabled(true);
				}
			}
		}

		private Model prepareModel() {
			StandardModel model = new StandardModel();
			for (ModelSupplier ms : modelSuppliers) {
				model.add(ms.getModel());
				model.add("startButton", start);
			}
			return model;
		}
	}

	private void setResume(boolean b, AxisController axisController) {
		axisController.getAnimation().setResume(b);
		isResume = b;
	}
}
